package com.qianfeng.zw.meilishuo;

import android.app.Application;

import com.jayqqaa12.abase.core.Abase;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;
import com.qianfeng.zw.meilishuo.config.AppConfig;
import com.qianfeng.zw.meilishuo.netstate.NetChangeObserver;
import com.qianfeng.zw.meilishuo.netstate.NetWorkUtil;
import com.qianfeng.zw.meilishuo.netstate.NetworkStateReceiver;

import org.androidannotations.annotations.EApplication;

import java.io.File;


/**
 * @Package com.qianfeng.zw.meilishuo
 * @作 用:
 * 1.联网
 * 2.数据库的操作
 * 3.图片加载
 * 4.网络状态监听
 * 5.Apk升级
 * 6.全局异常处理
 * 7.Activity的处理
 * 8.一些控件 方法的封装
 * @创 建 人: zhangwei
 * @日 期: 15/3/28 17:33
 * @修 改 人:
 * @日 期:
 */
@EApplication
public class BaseApp extends Application {
    private static BaseApp app;
    private NetChangeObserver changeObserver = new NetChangeObserver() {
        @Override
        public void onConnect(NetWorkUtil.NetType type) {
            super.onConnect(type);
        }

        @Override
        public void onDisConnect() {
            super.onDisConnect();
        }
    };

    public static BaseApp getInstance() {
        return app;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        //注册全局异常处理类
//        Thread.setDefaultUncaughtExceptionHandler(AppException.getAppExceptionHandler(this));
        //初始化ImageLoader框架全局参数配置
        initImageLoaderConfiguration();
        //初始化工具类
        Abase.setContext(getApplicationContext());
        initNetWork();
    }

    /**
     * 初始化网络
     */
    private void initNetWork() {
        NetworkStateReceiver.registerNetworkStateReceiver(this);
        NetworkStateReceiver.registerObserver(changeObserver);
    }


    /**
     * 处理Universl
     */
    private void initImageLoaderConfiguration() {
        File cacheDir = StorageUtils.getOwnCacheDirectory(getApplicationContext(), AppConfig.APP_CACHE + File.separator + AppConfig.IMAGE_CACHE);
        ImageLoaderConfiguration config = new ImageLoaderConfiguration
                .Builder(this)
                .memoryCacheExtraOptions(480, 800) // max width, max height，即保存的每个缓存文件的最大长宽
                .threadPoolSize(5)//线程池内加载的数量
                .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new UsingFreqLimitedMemoryCache(8 * 1024 * 1024)) // You can pass your own memory cache implementation/你可以通过自己的内存缓存实现
//                .memoryCacheSize(2 * 1024 * 1024)
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .diskCache(new UnlimitedDiscCache(cacheDir))//自定义缓存路径
                .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
                .imageDownloader(new BaseImageDownloader(this, 5 * 1000, 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间
//                .writeDebugLogs() // Remove for release app
                .build();//开始构建
        ImageLoader.getInstance().init(config);
    }

    public void unRegister() {
        if (changeObserver != null) {
            NetworkStateReceiver.removeRegisterObserver(changeObserver);
        }
        NetworkStateReceiver.unRegisterNetworkStateReceiver(this);
    }


    public class AppNetChangeObserver extends NetChangeObserver {

    }
}
